package com.liujun.spring4.demo.aop.ioc.base.applicationContextBeans;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * 进行调用测试
 *
 * @author liujun
 * @version 1.0
 * @date 2018-08-13 07:44:00
 */
public class BeanLiftCycle {

  private static void lifecycleinbeanfactory() {

    ApplicationContext context = new FileSystemXmlApplicationContext(
            "classpath:com/liujun/spring4/demo/ioc/base/applicationContextBeans/car-application.xml");

    // 4，第一次从容器中获取Car,将触发容器实例化该Bean,这将引发Bean生命周期方法的调用
    Car car1 = (Car) context.getBean("car");

    car1.introduce();
    car1.setColor("红色");

    // 第二次从容器中获取car直接从缓存池中获取
    Car car2 = (Car) context.getBean("car");

    // 查看car1和car2是否指向同一引用
    System.out.println("car1 == car2:" + (car1 == car2));

    // 关闭容器
    ((DefaultListableBeanFactory) ((FileSystemXmlApplicationContext) context).getBeanFactory()).destroySingletons();
  }

  public static void main(String[] args) {
    lifecycleinbeanfactory();
  }
}
